package cn.ac.iscas.service.transformer;

import cn.ac.iscas.base.utils.ParameterUtils;
import cn.ac.iscas.base.utils.SpringUtil;
import cn.ac.iscas.domain.flink.FlinkColumnType;
import cn.ac.iscas.domain.source.FlinkSourceColumn;
import cn.ac.iscas.service.queue.flink.FlinkTableQueueService;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/12/22 16:49
 */
public class NormalizationTransformer {
    private final static String ID = "normalization_trans";
    private final static String COLUMN_KEY = "column";
    private final static String LABEL_KEY = "label";
    private String jobId;
    private String componentId;
    private String frontTableName;
    private List<FlinkSourceColumn> frontColumns;

    private String currentTableName;
    private List<FlinkSourceColumn> currentColumns;

    public NormalizationTransformer(String jobId, String taskId) {
        this.jobId = jobId;
        this.componentId = taskId;
    }

    public NormalizationTransformer(String jobId, String taskId, String frontTableName, List<FlinkSourceColumn> frontColumns) {
        this.jobId = jobId;
        this.componentId = taskId;
        this.frontTableName = frontTableName;
        this.frontColumns = frontColumns;
        this.currentColumns = frontColumns;
        this.currentTableName = String.format("%s.%s.%s_%s", jobId, jobId, ID, UUID.randomUUID().toString().substring(0,6));
    }


    public NormalizationTransformer() {

    }

    //SELECT (price - (SELECT MIN(price) FROM ods_house_price2c))*1.0/((SELECT MAX(price) FROM ods_house_price2c)-(SELECT MIN(price) FROM ods_house_price2c)) AS normp FROM ods_house_price2c
    public String getSql(Map<String, Object> parameters) {
        String createViewSql = String.format("CREATE VIEW %s AS ", currentTableName);
        String column = ParameterUtils.getRequiredString(parameters, COLUMN_KEY);
        String label = ParameterUtils.getRequiredString(parameters, LABEL_KEY);
        String selectSqlTmp = String.format(" ( SELECT *, (%s - (SELECT MIN(%s) FROM %s))*1.0/((SELECT MAX(%s) FROM %s)-(SELECT MIN(%s) FROM %s)) AS %s FROM %s ) ",
                column, column, frontTableName, column, frontTableName, column, frontTableName, label, frontTableName);
        createViewSql += selectSqlTmp;

        FlinkSourceColumn newColumn = new FlinkSourceColumn();
        newColumn.setType(FlinkColumnType.DOUBLE.getName());
        newColumn.setName(label);
        newColumn.setPrimaryKey(false);
        this.currentColumns.add(newColumn);

        updateSource();
        return createViewSql;
    }


    public void updateSource() {
        FlinkTableQueueService flinkTableQueueService = SpringUtil.getBean(FlinkTableQueueService.class);
        Map<String, List<FlinkSourceColumn>> tableColumns = new ConcurrentHashMap<>(8);
        tableColumns.put(this.currentTableName, this.currentColumns);
        flinkTableQueueService.put(this.jobId, this.componentId, tableColumns);
    }
}
